﻿using BCCommon;
using BCEntity.TMS.Order.OrderEvaluation;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCData.TMS.Order.OrderEvaluation
{
    public class OrderEvaluationData : IOrderEvaluationData
    {
        private const string QUERY = @"SELECT OrderEvaluationId,OrderId,DriverId,UserId,IsUserEvaluated,IsVehicleEvaluated,VehicleEvaluation,
                                            UserEvaluation,UserEvaluationContent,VehicleEvaluationContent,UserEvaluationTime,VehicleEvaluationTime 
                                    FROM OrderEvaluation";
        private readonly IDatabaseContext databaseContext;

        public OrderEvaluationData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public async Task<OrderEvaluationEntity> Create(OrderEvaluationEntity orderEvaluationEntity)
        {
            string sql = SqlCommands.GetInsertSql("OrderEvaluation", new string[] { "OrderId", "DriverId", "UserId" });
            sql = string.Format("{0} {1} WHERE OrderEvaluationId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", orderEvaluationEntity.OrderId, DbType.Int64, size: 20);
            parameters.Add("?DriverId", orderEvaluationEntity.DriverId, DbType.Int64, size: 20);
            parameters.Add("?UserId", orderEvaluationEntity.UserId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderEvaluationEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<OrderEvaluationEntity> GetEntity(long orderId)
        {
            string sql = string.Format("{0} WHERE OrderId=?OrderId;", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", orderId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<OrderEvaluationEntity>(sql, parameters);
        }

        public async Task<OrderEvaluationEntity> UserEvaluate(OrderEvaluationEntity orderEvaluationEntity)
        {
            string sql = SqlCommands.GetUpdateSql("OrderEvaluation", new string[] { "IsUserEvaluated", "UserEvaluation", "UserEvaluationContent", "UserEvaluationTime" }, new string[] { "OrderId" });
            sql = string.Format("{0} {1} WHERE OrderId=?OrderId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", orderEvaluationEntity.OrderId, DbType.Int64, size: 20);
            parameters.Add("?IsUserEvaluated", true, DbType.Boolean);
            parameters.Add("?UserEvaluation", orderEvaluationEntity.UserEvaluation, DbType.Decimal);
            parameters.Add("?UserEvaluationContent", orderEvaluationEntity.UserEvaluationContent, DbType.AnsiString, size: 200);
            parameters.Add("?UserEvaluationTime", DateTime.Now, DbType.DateTime);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderEvaluationEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<OrderEvaluationEntity> VehicleEvaluate(OrderEvaluationEntity orderEvaluationEntity)
        {
            string sql = SqlCommands.GetUpdateSql("OrderEvaluation", new string[] { "IsVehicleEvaluated", "VehicleEvaluation", "VehicleEvaluationContent", "VehicleEvaluationTime" }, new string[] { "OrderId" });
            sql = string.Format("{0} {1} WHERE OrderId=?OrderId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderId", orderEvaluationEntity.OrderId, DbType.Int64, size: 20);
            parameters.Add("?IsVehicleEvaluated", true, DbType.Boolean);
            parameters.Add("?VehicleEvaluation", orderEvaluationEntity.VehicleEvaluation, DbType.Decimal);
            parameters.Add("?VehicleEvaluationContent", orderEvaluationEntity.VehicleEvaluationContent, DbType.AnsiString, size: 200);
            parameters.Add("?VehicleEvaluationTime", DateTime.Now, DbType.DateTime);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderEvaluationEntity>(sql, parameters, databaseContext.Transaction);
        }
    }
}
